/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * FrmDistrubution.java
 *
 * Created on 06-ago-2009, 20:08:52
 */
package gui.forms;

import gui.tools.Receiver;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import lib.model.distribution.*;

/**
 *
 * @author jorge
 */
public class FrmDistrubution extends javax.swing.JDialog {

    private Distribution distribution;
    private Receiver parent;

    /** Creates new form FrmDistrubution */
    public FrmDistrubution(Distribution distribution, Receiver parent) {
        initComponents();
        this.getRootPane().setDefaultButton(btnSave);

        String fileString = "/res/icons/logo.png";
        try {
            Image icon = ImageIO.read(getClass().getResource(fileString));
            this.setIconImage(icon);
        } catch (java.io.IOException ioe) {
            System.out.println("Error loading file: " + fileString);
        }

        this.parent = parent;
        this.distribution = distribution;
        for (DistributionType t : DistributionType.values()) {
            cbType.addItem(t);
        }
        if (distribution != null) {
            cbType.setSelectedItem(distribution.getType());
            int meanHours = (int) distribution.getMean() / 3600000;
            int meanMinutes = (int) (distribution.getMean() % 3600000) / 60000;
            txtMeanHours.setText(String.valueOf(meanHours));
            txtMeanMinutes.setText(String.valueOf(meanMinutes));
            if (distribution.getType() != DistributionType.STATIC) {
                int varianceHours = (int) distribution.getVariance() / 3600000;
                int varianceMinutes = (int) (distribution.getVariance() % 3600000) / 60000;
                txtVarianceHours.setText(String.valueOf(varianceHours));
                txtVarianceMinutes.setText(String.valueOf(varianceMinutes));
            }
            this.setTitle("Edit parameter:");
        } else {
            this.setTitle("New parameter:");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblType = new javax.swing.JLabel();
        lblMean = new javax.swing.JLabel();
        lblVariance = new javax.swing.JLabel();
        cbType = new javax.swing.JComboBox();
        txtMeanHours = new javax.swing.JTextField();
        txtVarianceHours = new javax.swing.JTextField();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        lblMeanColon = new javax.swing.JLabel();
        lblVarianceColon = new javax.swing.JLabel();
        txtVarianceMinutes = new javax.swing.JTextField();
        txtMeanMinutes = new javax.swing.JTextField();

        setModal(true);
        setResizable(false);

        lblType.setText("Type:");

        lblMean.setText("Mean:");

        lblVariance.setText("Variance:");

        cbType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTypeActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        lblMeanColon.setText(":");

        lblVarianceColon.setText(":");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblType)
                            .addComponent(lblMean)
                            .addComponent(lblVariance))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbType, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtVarianceHours, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtMeanHours, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(lblVarianceColon, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(lblMeanColon, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtMeanMinutes)
                                    .addComponent(txtVarianceMinutes, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblType)
                    .addComponent(cbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMean)
                    .addComponent(txtMeanHours, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblMeanColon)
                    .addComponent(txtMeanMinutes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblVariance)
                    .addComponent(txtVarianceHours, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblVarianceColon)
                    .addComponent(txtVarianceMinutes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnSave))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-234)/2, (screenSize.height-166)/2, 234, 166);
    }// </editor-fold>//GEN-END:initComponents

    private void cbTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTypeActionPerformed
        boolean dstat = DistributionType.valueOf(cbType.getSelectedItem().toString()) !=
                DistributionType.STATIC;
        txtVarianceHours.setEnabled(dstat);
        txtVarianceMinutes.setEnabled(dstat);
        txtVarianceMinutes.setEnabled(dstat);
        lblVarianceColon.setEnabled(dstat);
        lblVariance.setEnabled(dstat);
    }//GEN-LAST:event_cbTypeActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (txtMeanHours.getText().equals("")) {
            txtMeanHours.setText("0");
        }
        if (txtMeanMinutes.getText().equals("")) {
            txtMeanMinutes.setText("0");
        }
        if (txtVarianceHours.getText().equals("")) {
            txtVarianceHours.setText("0");
        }
        if (txtVarianceMinutes.getText().equals("")) {
            txtVarianceMinutes.setText("0");
        }
        try {
            double mean = Double.parseDouble(txtMeanHours.getText()) * 3600000 +
                    Double.parseDouble(txtMeanMinutes.getText()) * 60000;
            double variance = Double.parseDouble(txtVarianceHours.getText()) * 3600000 +
                    Double.parseDouble(txtVarianceMinutes.getText()) * 60000;
            if (mean > 0) {
                switch (DistributionType.valueOf(cbType.getSelectedItem().toString())) {
                    case EXPONENTIAL:
                        distribution = new ExponentialDistribution(mean, variance);
                        break;
                    case NORMAL:
                        distribution = new NormalDistribution(mean, variance);
                        break;
                    case STATIC:
                        distribution = new StaticDistribution(mean);
                        break;
                    case UNIFORM:
                        distribution = new UniformDistribution(mean, variance);
                        break;
                }
                parent.reciveObject(distribution);
                this.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(rootPane,
                        "Error:\n Mean must be bigger than zero.",
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        } catch (NumberFormatException nfe) {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Mean must be bigger than zero.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbType;
    private javax.swing.JLabel lblMean;
    private javax.swing.JLabel lblMeanColon;
    private javax.swing.JLabel lblType;
    private javax.swing.JLabel lblVariance;
    private javax.swing.JLabel lblVarianceColon;
    private javax.swing.JTextField txtMeanHours;
    private javax.swing.JTextField txtMeanMinutes;
    private javax.swing.JTextField txtVarianceHours;
    private javax.swing.JTextField txtVarianceMinutes;
    // End of variables declaration//GEN-END:variables
}
